package com.bootdo.act.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.act.dao.ActTeamDao;
import com.bootdo.act.dao.InviteRecordDao;
import com.bootdo.act.domain.ActTeamDO;
import com.bootdo.act.domain.InviteRecordDO;
import com.bootdo.act.service.ActTeamService;



@Service
public class ActTeamServiceImpl implements ActTeamService {
	@Autowired
	private ActTeamDao actTeamDao;
	@Autowired
	private InviteRecordDao inviteRecordDao;
	
	@Override
	public ActTeamDO get(Long id){
		return actTeamDao.get(id);
	}
	
	@Override
	public List<ActTeamDO> list(Map<String, Object> map){
		return actTeamDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return actTeamDao.count(map);
	}
	
	@Override
	public int save(ActTeamDO actTeam){
		int r = actTeamDao.save(actTeam);
		List<InviteRecordDO> records = new ArrayList<>();
		String[] userNames = actTeam.getUserNames();
		Long[] userIds = actTeam.getUserIds();
		for (int i = 0; i < userIds.length; i++) {
			InviteRecordDO inviteRecord = new InviteRecordDO();
			inviteRecord.setTeamName(actTeam.getName());
			inviteRecord.setTeamId(actTeam.getId());
			inviteRecord.setInviteDate(new Date());
			inviteRecord.setInviterId(actTeam.getCreaterId());
			inviteRecord.setInviterName(actTeam.getCreaterName());
			inviteRecord.setUserId(userIds[i]);
			inviteRecord.setUserName(userNames[i]);
			records.add(inviteRecord);
		}
		inviteRecordDao.batchSave(records);
		return r;
	}
	
	@Override
	public int update(ActTeamDO actTeam){
		return actTeamDao.update(actTeam);
	}
	
	@Override
	public int remove(Long id){
		return actTeamDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return actTeamDao.batchRemove(ids);
	}
	
}
